import api from '../utils/api';

// 家属关系类型定义
export interface FamilyRelation {
  id: number;
  elderlyId: number;
  elderlyName: string;
  familyId: number;
  familyName: string;
  relationship: string; // 关系描述
  status: number; // 1-待确认，2-已确认，3-已解除
  createTime: string;
  updateTime: string;
}

// 创建家属关系请求参数
export interface CreateFamilyRelationRequest {
  elderlyId: number;
  familyId: number;
  relationship: string;
}

// 家属关系服务类
class FamilyRelationService {
  // 创建家属关系申请
  async createRelationRequest(params: CreateFamilyRelationRequest): Promise<FamilyRelation> {
    return await api.post('/family/create', params);
  }

  // 确认家属关系
  async confirmRelation(id: number): Promise<FamilyRelation> {
    return await api.put(`/family/confirm/${id}`);
  }

  // 解除家属关系
  async terminateRelation(id: number): Promise<FamilyRelation> {
    return await api.put(`/family/terminate/${id}`);
  }

  // 获取老人的家属列表
  async getElderlyFamilies(elderlyId: number): Promise<FamilyRelation[]> {
    return await api.get(`/family/elderly/${elderlyId}`);
  }

  // 获取家属关联的老人列表
  async getFamilyElderlies(familyId: number): Promise<FamilyRelation[]> {
    return await api.get(`/family/elderly-by-family/${familyId}`);
  }

  // 检查家属关系是否存在
  async checkRelationExists(elderlyId: number, familyId: number): Promise<boolean> {
    return await api.get('/family/check', {
      elderlyId,
      familyId
    });
  }

  // 获取待确认的家属关系申请
  async getPendingRelations(): Promise<FamilyRelation[]> {
    return await api.get('/family/pending');
  }

  // 获取家属关系详情
  async getRelationDetail(id: number): Promise<FamilyRelation> {
    return await api.get(`/family/detail/${id}`);
  }
}

export default new FamilyRelationService();